#!/bin/bash
set -euo pipefail
BaseDir=$(cd "$(dirname "$0")"; pwd)
cd ${BaseDir}

# udocker不支持从Dockerfile做镜像，不过可以将其他镜像放到pkg下，udocker load加载到本机运行
case "$1" in
mysql)
    echo "run mysql..."
    #  between 0 and 5 to the environment variable UDOCKER_LOGLEVEL.
    # UDOCKER_LOGLEVEL=5 udocker pull docker.m.daocloud.io/mysql:8.0.28-oracle
    # udocker load -i ./pkg/mysql8.tar; udocker images
    udocker run --name=mysql -e MYSQL_ROOT_PASSWORD=Demo12# -p 3306:3306 --rm docker.m.daocloud.io/mysql:8.0.28-oracle
;;
mysql-cli)
    echo "mysql命令行... show processlist; exit"
    udocker run mysql bash -c "mysql -uroot -pDemo12# -s"
;;

redis)
    echo "run redis..."
    # UDOCKER_LOGLEVEL=5 udocker pull docker.m.daocloud.io/redis:5.0
    udocker run --name=redis -p 6379:6379 docker.m.daocloud.io/redis:5.0
;;

jdk)
    udocker run registry.cn-beijing.aliyuncs.com/wushifeng/thingstrue:openjdk_8 /bin/bash
;;

python)
   # https://github.com/docker-library/docs/tree/master/python
   udocker run python:3.13.0-alpine3.20
;;

clean-all)
    echo "停止当前运行的所有容器服务..."
    udocker ps | awk 'NR>1 {print $1}'|xargs -I {} bash -c "echo {} to delete; udocker rm {}" 
    udocker ps
;;

*)
    cat <<EOF
MYSQL: ./svc.sh mysql mysql-cli
REDIS: ./svc.sh redis

./svc.sh clean-all
EOF
esac
